package edu.nf.supermarket.service.impl;

import edu.nf.supermarket.entity.CommentImage;
import edu.nf.supermarket.entity.Comments;
import edu.nf.supermarket.mapper.CommentMapper;
import edu.nf.supermarket.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yjj
 * @date 2025-06-10
 */
@Service("commentService")
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 获取所有评论
     *
     * @param productId 商品Id
     * @return
     */
    @Override
    public List<Comments> listComment(Integer productId) {
        List<Comments> comments = commentMapper.listComment(productId);
        comments.forEach(c -> {
            c.setCommentImages(commentMapper.listCommentImage(c.getCommentId()));
        });
        return comments;
    }

    /**
     * 根据评分获取评论
     *
     * @param grade
     * @param commodityId
     * @return
     */
    @Override
    public List<Comments> findCommentGoofOrBad(Byte grade, Integer commodityId) {
        List<Comments> comments = switch (grade) {
            case 4, 5 -> commentMapper.findGoodComment(commodityId);
            case 3 -> commentMapper.findMiddleComment(commodityId);
            case 2, 1 -> commentMapper.findBadComment(commodityId);
            default -> commentMapper.listComment(commodityId);
        };
        comments.forEach(c -> {
            c.setCommentImages(commentMapper.listCommentImage(c.getCommentId()));
        });
        return comments;

    }

    @Override
    public List<Comments> listCommentByDESC(Integer productId) {
        return commentMapper.findCommentByDESC(productId);
    }

    @Override
    public void deleteComment(Integer commentId) {
        commentMapper.deleteComment(commentId);
    }

    @Override
    public List<Comments> listCommentByUserId(Integer userId) {
        List<Comments> comments = commentMapper.listCommentByUserId(userId);
        comments.forEach(c -> {
            c.setCommentImages(commentMapper.listCommentImage(c.getCommentId()));
        });
        return comments;
    }

    @Override
    public Integer addComment(Comments comments, Integer userId, String orderId) {
        commentMapper.addComment(comments,  userId, orderId);
        return comments.getCommentId();

    }

    @Override
    public void addCommentImages(Integer commentId,CommentImage commentImage) {
        commentMapper.addCommentImages(commentId,commentImage);
    }


}
